import 'package:flutter/material.dart';

class Fit {
  static double width;
  static double height;
  static double w;
  static double h;
  static double scaledDensity;
  static MediaQueryData _mediaQueryData;
  static double _textScaleFactor;

  static void init(BuildContext context, int d_width, int d_height) {
    MediaQueryData mediaQuery = MediaQuery.of(context);
    _mediaQueryData = mediaQuery;
    width = d_width.toDouble();
    height = d_height.toDouble();
    w = mediaQuery.size.width;
    h = mediaQuery.size.height;
    scaledDensity = mediaQuery.devicePixelRatio;
    _textScaleFactor = mediaQuery.textScaleFactor;
    print('scaledDensity:  $scaledDensity');
  }

  static double scaleX(int x) {
    return scaleXX(x.toDouble(), 0.0);
  }

  static double scaleY(int y) {
    return scaleYY(y.toDouble(), 0.0);
  }

  static double scaleXX(double x, double ox) {
    return (x * (w - w * ox) / (width - width * ox));
  }

  static double scaleYY(double y, double oy) {
    return (y * (h - h * oy) / (height - height * oy));
  }

  static double getWidth() {
    return w;
  }

  static double getHeight() {
    return h;
  }

  static double scale(double x) {
    if (w < h) {
      return x * w / width;
    } else {
      return x * h / width;
    }
  }

  static double setWidth(int width) {
    return width * (w / width);
  }

  static double scaleTextSize(int textSize, [allowFontScaling = true]) {
    return allowFontScaling
        ? textSize * (w / width) * _textScaleFactor
        : textSize * (w / width);
  }
}
